package main.com.hjy.jdbc.dao.impl;

import main.com.hjy.jdbc.dao.RoomInfoDao;
import main.com.hjy.jdbc.entity.RoomInfo;
import main.com.hjy.jdbc.utils.DBPoolUtils;
import main.com.hjy.jdbc.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomInfoDaoImpl implements RoomInfoDao {

    @Override
    public boolean addRoomInfo(RoomInfo roominfo) {
        Connection con = DBPoolUtils.getConnection();

        try {
            if (con != null) {
                String sql = "insert into roominfo values(?,?,?,?,?,?)";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setString(1, roominfo.getRoomid());
                ps.setString(2, roominfo.getRoomname());
                ps.setString(3, roominfo.getRoomtypeid());
                ps.setDouble(4, roominfo.getPrice());
                ps.setInt(5, roominfo.getStatus());
                ps.setInt(6, roominfo.getCapacity());

                int num = ps.executeUpdate();
                if (num > 0) {
                    return true;
                } else {
                    return false;
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean deleteRoomInfo(RoomInfo roominfo) {
        Connection con = DBPoolUtils.getConnection();
        try {
            if (con != null) {
                String sql = "delete from roominfo where roomid=?";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setString(1, roominfo.getRoomid());
                int rs = ps.executeUpdate();
                if (rs > 0) {
                    return true;
                } else {
                    return false;
                }

            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public List<RoomInfo> queryAllRoomInfo() {
        List<RoomInfo> list = new ArrayList<RoomInfo>();
        Connection con = DBPoolUtils.getConnection();
        try {
            if (con != null) {
                String sql = "select * from roominfo";
                PreparedStatement ps = con.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    list.add(new RoomInfo(rs.getString(1),
                            rs.getString(2),
                            rs.getString(3),
                            rs.getDouble(4),
                            rs.getInt(5),
                            rs.getInt(6)
                    ));

                }
                return list;
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    @Override
    public boolean updateRoomInfo(String roomid, String roomname, String roomtypeid, Double price, Integer status, Integer capacity) {
        Connection con = DBPoolUtils.getConnection();

        try {
            if (con != null) {
                String sql = "update roominfo set roomname=?,roomtypeid=?,price=?,status=?,capacity=? where roomid=?";
                PreparedStatement pstmt = con.prepareStatement(sql);
                pstmt.setString(1, roomname);
                pstmt.setString(2, roomtypeid);
                pstmt.setDouble(3, price);
                pstmt.setInt(4, status);
                pstmt.setInt(5, capacity);
                int num = pstmt.executeUpdate();
                if (num > 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}


